using AhpilyServer;
using GameServer.Concurrent;
using GameServer.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GameServer.Cache
{
    /// <summary>
    /// 角色数据缓存层
    ///     存账号的
    /// </summary>
    public class UserCache
    {
        /// <summary>
        /// 角色的id
        /// </summary>
        ConcurrentInt id = new ConcurrentInt(-1);

        /// <summary>
        ///  角色ID 对应的 角色数据模型
        /// </summary>
        private Dictionary<int, UserModel> idModelDict = new Dictionary<int, UserModel>();

        /// <summary>
        /// 账号Id 与 角色 Id 的映射
        /// </summary>
        private Dictionary<int, int> accIdUIDDict = new Dictionary<int, int>();
        /// <summary>
        /// 创建角色
        /// </summary>
        public void Create(string name, int accountId)
        {
            UserModel model = new UserModel(id.Add_Get(), name, accountId);
            //保存到字典里

            idModelDict.Add(model.Id, model);
            accIdUIDDict.Add(accountId, model.Id);
        }

        /// <summary>
        /// 判断此账号是否有角色数据
        /// </summary>
        public bool IsExit(int accountID)
        {
            return accIdUIDDict.ContainsKey(accountID);
        }

        /// <summary>
        /// 根据账号id获得角色数据
        /// </summary>
        public UserModel GetModelByAccountId(int accountId)
        {
            int modelId = accIdUIDDict[accountId];
            UserModel model = idModelDict[modelId];
            return model;
        }

        /// <summary>
        /// 通过账号id 获取 角色id
        /// </summary>
        /// <param name="accountId"></param>
        /// <returns></returns>
        public int GetId(int accountId)
        {
            int modelId = accIdUIDDict[accountId];
            return modelId;
        }

        /// <summary>
        /// 存储在线玩家
        /// </summary>
        private Dictionary<int, ClientPeer> idClientDict = new Dictionary<int, ClientPeer>();
        private Dictionary<ClientPeer, int> clientIdDict = new Dictionary<ClientPeer, int>();


        /// <summary>
        /// 判断用户是否在线
        /// </summary>
        public bool IsOnline(ClientPeer clientPeer)
        {
            return clientIdDict.ContainsKey(clientPeer);
        }
        /// <summary>
        /// 判断用户是否在线
        /// </summary>
        public bool IsOnline(int id)
        {
            return idClientDict.ContainsKey(id);
        }

        /// <summary>
        /// 上线
        /// </summary>
        public void Online(ClientPeer client, int id)
        {
            idClientDict.Add(id, client);
            clientIdDict.Add(client, id);
        }
        /// <summary>
        ///  更新角色数据
        /// </summary>
        public void Update(UserModel model)
        {
            idModelDict[model.Id] = model;
        }
        /// <summary>
        /// 下线
        /// </summary>
        public void OffLine(ClientPeer client)
        {
            int id = clientIdDict[client];
            idClientDict.Remove(id);
            clientIdDict.Remove(client);
        }
        /// <summary>
        /// 通过超链接对象获取角色模型
        /// </summary>
        public UserModel GetModelByClientPeer(ClientPeer client)
        {
            int id = clientIdDict[client];
            return idModelDict[id];
        }
        public UserModel GetModelById(int userId)
        {
            return idModelDict[userId];
        }
        /// <summary>
        /// 通过角色id获取角色模型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ClientPeer GetClientPeer(int id)
        {
            return idClientDict[id];
        }

        /// <summary>
        /// 根据在线玩家的连接对象 获取 角色id
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        public int GetId(ClientPeer client)
        {
            if(!clientIdDict.ContainsKey(client))
            {
                throw new IndexOutOfRangeException("角色不在线");
            }
            return clientIdDict[client];
        }


    }
}
